{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Standard Type Library"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandapower provides a standard type library that allows the comfortable managing of type data for lines and transformers.\n",
    "\n",
    "The following examples are all based on lines, but can be equally used for the transformer database by specifying element=\"trafo\" instead of element=\"line\" in all standard type functions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Pandapower Standard Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandapower comes with generic standard types that you can use to build generic networks:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:47.270369Z",
     "start_time": "2025-10-20T12:29:36.749853Z"
    }
   },
   "outputs": [],
   "source": [
    "from pandapower.create import create_empty_network, create_bus, create_line\n",
    "from pandapower.std_types import (\n",
    "    available_std_types,\n",
    "    create_std_type,\n",
    "    create_std_types,\n",
    "    copy_std_types,\n",
    "    parameter_from_std_type,\n",
    "    change_std_type,\n",
    "    find_std_type_by_parameter\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:47.791059Z",
     "start_time": "2025-10-20T12:29:47.285907Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "line_std_type = \"NAYY 4x50 SE\"\n",
    "\n",
    "net = create_empty_network()\n",
    "available_std_types(net, element=\"line\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you create a line with the create_line function, the element parameters are taken from this library:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:48.026416Z",
     "start_time": "2025-10-20T12:29:47.962268Z"
    }
   },
   "outputs": [],
   "source": [
    "b1 = create_bus(net, vn_kv=.4)\n",
    "b2 = create_bus(net, vn_kv=.4)\n",
    "lid = create_line(net, from_bus=b1, to_bus=b2, length_km=0.1, std_type=line_std_type, name=\"test_line\")\n",
    "net.line.loc[lid]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Individual Standard Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Additionally to the basic pandapower standard types, you can define individual standard types for you pandapower network.\n",
    "\n",
    "To create a new standard type you need at least the following parameters:\n",
    "\n",
    "**For Lines**:\n",
    "- r_ohm_per_km\n",
    "- x_ohm_per_km\n",
    "- c_nf_per_km\n",
    "- max_i_ka\n",
    "\n",
    "**For Transformers:**\n",
    "- sn_mva\n",
    "- vn_hv_kv\n",
    "- vn_lv_kv\n",
    "- vk_percent\n",
    "- vkr_percent\n",
    "- i0_percent\n",
    "- pfe_kw\n",
    "- shift_degree\n",
    "\n",
    "To define the parameters, create a dictionary with the style {\"parameter1\": parameter_value1, \"parameter2\": paramter_value2,...} and save the standard type to your network with the create_std_type function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:50.226921Z",
     "start_time": "2025-10-20T12:29:49.320941Z"
    }
   },
   "outputs": [],
   "source": [
    "net = create_empty_network()\n",
    "test_type = {\"r_ohm_per_km\": 0.01, \"x_ohm_per_km\": 0.02, \"c_nf_per_km\": 10, \"max_i_ka\": 0.4, \"type\": \"cs\"}\n",
    "create_std_type(net, name=\"test_type\", data=test_type, element=\"line\")\n",
    "available_std_types(net, element=\"line\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see the test_type is now available in the standard type library, and you can use it to create a line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:51.021793Z",
     "start_time": "2025-10-20T12:29:50.952785Z"
    }
   },
   "outputs": [],
   "source": [
    "b1 = create_bus(net, vn_kv=.4)\n",
    "b2 = create_bus(net, vn_kv=.4)\n",
    "lid = create_line(net, from_bus=b1, to_bus=b2, length_km=0.1, std_type=\"test_type\", name=\"test_line\")\n",
    "print(net.line.loc[lid])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Local Standard Type Libraries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Standard tpyes are saved with the network if you use the to_json function and are therefore still available if you load the network again.\n",
    "\n",
    "If you have a set of standard types that you want to save locally and apply to multiple networks, you can create a set of standard type parameters as a dictionary in the style {\"std_type1: {\"r_ohm_per_km\": r_ohm_per_km,...}, \"std_type2\": {\"r_ohm_per_km\": r_ohm_per_km,...},...} and add it to the network with the create_std_types function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:52.435795Z",
     "start_time": "2025-10-20T12:29:51.309004Z"
    }
   },
   "outputs": [],
   "source": [
    "net = create_empty_network()\n",
    "linetypes = {\"typ1\": {\"r_ohm_per_km\": 0.01, \"x_ohm_per_km\": 0.02, \"c_nf_per_km\": 10, \"max_i_ka\": 0.4, \"type\": \"cs\"},\n",
    "             \"typ2\": {\"r_ohm_per_km\": 0.015, \"x_ohm_per_km\": 0.01, \"c_nf_per_km\": 30, \"max_i_ka\": 0.3, \"type\": \"cs\"}}\n",
    "create_std_types(net, data=linetypes, element=\"line\")\n",
    "available_std_types(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The linetypes dictionary can be easily saved in a local project folder (e.g. using pickle or jsonpickle) and reloaded once it is needed. \n",
    "\n",
    "Another possibility is to copy the standard type library of an existing network:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:54.894676Z",
     "start_time": "2025-10-20T12:29:53.654707Z"
    }
   },
   "outputs": [],
   "source": [
    "new_net = create_empty_network()\n",
    "copy_std_types(from_net=net, to_net=new_net, element=\"line\")\n",
    "available_std_types(new_net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add Additional Parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "You can define additional parameters in the standard type library and load them into pandapower. For example, if you want to run a reliability analysis for which you need a hazard rate for each line, you will want to add the hazard rate \"h_per_km\" for your individual standard type:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:29:57.061749Z",
     "start_time": "2025-10-20T12:29:55.795218Z"
    }
   },
   "outputs": [],
   "source": [
    "net = create_empty_network()\n",
    "test_type = {\"r_ohm_per_km\": 0.01, \"x_ohm_per_km\": 0.02, \"c_nf_per_km\": 10, \"max_i_ka\": 0.4, \"type\": \"cs\", \"h_per_km\": 0.01}\n",
    "create_std_type(net, name=\"test_type\", data=test_type, element=\"line\")\n",
    "available_std_types(net, element=\"line\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can see that the parameter \"h_per_km\" is stored in the standard type database for the new standard type.\n",
    "\n",
    "We now create two lines, one of the new \"test_type\" and one with a standard type that does not have the parameter \"h_per_km\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:30:00.070221Z",
     "start_time": "2025-10-20T12:29:59.800224Z"
    }
   },
   "outputs": [],
   "source": [
    "b1 = create_bus(net, vn_kv=.4)\n",
    "b2 = create_bus(net, vn_kv=.4)\n",
    "l1 = create_line(net, from_bus=b1, to_bus=b2, length_km=0.1, std_type=\"test_type\", name=\"test_line1\")\n",
    "l2 = create_line(net, from_bus=b1, to_bus=b2, length_km=0.1, std_type=line_std_type, name=\"test_line2\")\n",
    "net.line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The parameter \"h_per_km\" ist not in the line table, since it is not a pandapower standard parameter. You can however load the parameter from the database with the parameter_from_std_type function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:30:01.229785Z",
     "start_time": "2025-10-20T12:30:01.147104Z"
    }
   },
   "outputs": [],
   "source": [
    "parameter_from_std_type(net, parameter=\"h_per_km\")\n",
    "net.line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandapower adds the parameter to the linetable where it is available. The second line does not have a value for h_per_km, since it is not specified in the standard type database. You can however define a fallback (fill) value for lines that do not have a standard type or that have a standard type for which the desired parameter is not available:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:30:02.117028Z",
     "start_time": "2025-10-20T12:30:02.018565Z"
    }
   },
   "outputs": [],
   "source": [
    "parameter_from_std_type(net, parameter=\"h_per_km\", fill=0.02)\n",
    "net.line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change Standard Type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The change_std_type function allows changing the standard type of an element."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:30:04.470630Z",
     "start_time": "2025-10-20T12:30:03.268067Z"
    }
   },
   "outputs": [],
   "source": [
    "net = create_empty_network()\n",
    "b1 = create_bus(net, vn_kv=.4)\n",
    "b2 = create_bus(net, vn_kv=.4)\n",
    "l2 = create_line(net, from_bus=b1, to_bus=b2, length_km=0.1, std_type=line_std_type, name=\"test_line\")\n",
    "net.line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:30:04.937936Z",
     "start_time": "2025-10-20T12:30:04.862098Z"
    }
   },
   "outputs": [],
   "source": [
    "change_std_type(net, l2, \"NAYY 4x150 SE\", element=\"line\")\n",
    "net.line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find Standard Type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "It is possible to search for a standard type given a set of parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:30:05.918591Z",
     "start_time": "2025-10-20T12:30:05.881861Z"
    }
   },
   "outputs": [],
   "source": [
    "find_std_type_by_parameter(net, data={\"r_ohm_per_km\": 0.1188, \"x_ohm_per_km\": 0.39})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "You can also define a tolerance epsilon to find types with similar parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T12:30:06.417733Z",
     "start_time": "2025-10-20T12:30:06.400522Z"
    }
   },
   "outputs": [],
   "source": [
    "find_std_type_by_parameter(net, data={\"r_ohm_per_km\": 0.12, \"x_ohm_per_km\": 0.40}, epsilon=.05)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
